﻿using PwjCy.Lite.EntityLite;
using PwjCy.Lite.EntityLite.Imp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PwjCy.Lite.Persistence.Imp.SqlServer
{

    /// <summary>
    /// SqlServerBuilder
    /// </summary>
    public class SqlServerBuilder : SqlBuilderHandler
    {
        private const string insertSql = "INSERT INTO [dbo].[{0}]({1}) VALUES({2});";
        private const string deleteSql = "DELETE From [dbo].[{0}] where [Id]=@Id;";
        private const string updateSql = "UPDATE [dbo].[{0}] Set {1} where [Id]=@Id;";
        private const string selectSql = "SELECT {0} from {1} where [Id]=@Id";
        protected override string InsertSql(Type pwjcyObjectType, string tableName)
        {
            return string.Format(insertSql,
                    tableName,
                    DataFieldOutputSqlHandler(pwjcyObjectType, DelegateFieldTypeEnum.Insert),
                    DataFieldOutputSqlHandler(pwjcyObjectType, DelegateFieldTypeEnum.InsertParm));
        }
        protected override string DeleteSql(Type pwjcyObjectType, string tableName)
        {
            return string.Format(deleteSql, tableName);
        }
        protected override string UpdateSql(Type pwjcyObjectType, string tableName)
        {
            return string.Format(updateSql, tableName, DataFieldOutputSqlHandler(pwjcyObjectType, DelegateFieldTypeEnum.UpdateParm));
        }
        public override string LoadPwjCyObjectSql(Type pwjcyObjectType, string tableName)
        {
            return string.Format(selectSql, DataFieldOutputSqlHandler(pwjcyObjectType, DelegateFieldTypeEnum.Select), tableName);
        }
        protected override void DataFieldJoinWithComma(ref string outStr, PropertyInfo pro)
        {
            outStr += ",[" + pro.Name + "]";
        }
        protected override void DataFieldParmJoinWithComma(ref string outStr, PropertyInfo pro)
        {
            outStr += ",@" + pro.Name;
        }
        protected override void UpdateDataFieldParmJoinWithComma(ref string outStr, PropertyInfo pro)
        {
            outStr += ",[" + pro.Name + "]=@" + pro.Name;
        }
    }
}
